package com.tuling.core.loader;

import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import com.tuling.core.plugin.PluginInfo;

/**
 * 自定义classloader,加载外部jar包
 * @author lenovo
 *
 */
public class MyClassLoader extends URLClassLoader{
	
	private static MyClassLoader instance;
	private static File  pluginDir = null;
	
	public static MyClassLoader getClassLoader(String webappHome){
		if(null == instance){
			pluginDir = new File(webappHome + File.separator + "plugins");
			URL[] urls = new URL[] {};
			instance = new MyClassLoader(urls);
		}
		return instance;
	}
	
	@SuppressWarnings("resource")
	public PluginInfo addJar(File jarFile){
		PluginInfo info = new PluginInfo();
		try {
			this.addURL(jarFile.toURI().toURL());
			JarFile jar = new JarFile(jarFile);
			Manifest mf = jar.getManifest();
			String pluginName = mf.getMainAttributes().getValue("PluginName");
			String pluginClass = mf.getMainAttributes().getValue("PluginClass");
			String pluginDesc = mf.getMainAttributes().getValue("PluginDesc");
			info.setPluginClass(pluginClass);
			info.setPluginName(pluginName);
			info.setPluginDesc(pluginDesc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return info;
	}
	
	private MyClassLoader(URL [] urls){
		//加载外部jar文件
    	super(urls);
        File[] jars = pluginDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                boolean accept = false;
                String smallName = name.toLowerCase();
                if (smallName.endsWith(".jar")) {
                    accept = true;
                }
                return accept;
            }
        });
        
        if (jars == null) {
            return;
        }
		try {
			for (int i = 0; i < jars.length; i++) {
				if (jars[i].isFile()) {
					try {
						addURL(jars[i].toURI().toURL());
					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
	
	@SuppressWarnings("resource")
	public List<PluginInfo> loadPlugins(){
		File[] jars = pluginDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                boolean accept = false;
                String smallName = name.toLowerCase();
                if (smallName.endsWith(".jar")) {
                    accept = true;
                }
                return accept;
            }
        });
        
        if (jars == null) {
            return null;
        }
        
        List<PluginInfo> list = new ArrayList<>();
		try {
			for (int i = 0; i < jars.length; i++) {
				if (jars[i].isFile()) {
					JarFile jar = new JarFile(jars[i]);
					Manifest mf = jar.getManifest();
					String pluginName = mf.getMainAttributes().getValue("PluginName");
					String pluginClass = mf.getMainAttributes().getValue("PluginClass");
					String pluginDesc = mf.getMainAttributes().getValue("PluginDesc");
					PluginInfo info = new PluginInfo();
					info.setPluginClass(pluginClass);
					info.setPluginName(pluginName);
					info.setStatus(1);
					info.setPluginDesc(pluginDesc);
					list.add(info);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
